$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'vipcard/vipcard/list',
        datatype: "json",
        colModel: [			
			{ label: '卡号', name: 'id', index: 'id', width: 50, key: true },
			{ label: '卡密', name: 'password', index: 'password', width: 80 },
			{ label: '是否使用', name: 'isUsed', index: 'is_used', width: 80 ,
                formatter: function (data,index,row) {
                    var re;

                    if(data==0)
                        re="<div><label>未使用</label></div>"
                    else
                        re= "<div ><label>已使用</label></div>";
                    return re;

                } },
            { label: '生成时间', name: 'createDate', index: 'create_date', width: 80},
			// { label: '', name: 'userId', index: 'user_id', width: 80 },
			{ label: '绑定用户', name: 'username', index: 'username', width: 80 },
            { label: '绑定手机', name: 'mobile', index: 'mobile', width: 80 },

			{ label: '使用时间', name: 'useDate', index: 'use_date', width: 80 },

        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        number:0,
		vipCard: {},
        searchDate:'',
		isUsed:0,
        options:[
			{
				id:0,
				value:"未使用"
			},{id:1,
                value:"已使用"}
		],
        pickerOptions2: {
            shortcuts: [{
                text: '最近一周',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                    picker.$emit('pick', [start, end]);
                }
            }, {
                text: '最近一个月',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                    picker.$emit('pick', [start, end]);
                }
            }, {
                text: '最近三个月',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                    picker.$emit('pick', [start, end]);
                }
            }]
        },
        value6: '',
        value7: ''
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.vipCard = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.vipCard.id == null ? "vipcard/vipcard/save" : "vipcard/vipcard/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.number),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "vipcard/vipcard/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "vipcard/vipcard/info/"+id, function(r){
                vm.vipCard = r.vipCard;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
			postData:{'isUsed': vm.isUsed,
						'searchDate':vm.searchDate
			},
            }).trigger("reloadGrid");
		}
	}
});